/*
Table 6: Amount of Assistance to Community Members
Table A3: Counterpart of Table 6 with top 1% of observations of the outcome variable winsorised
Table A6: Counterpart of Table 6 with HTE (heterogeneous effects by baseline household asset wealth)
*/

est clear
version 14.0
set more off


// DATA
use "household_level_final.dta", clear
iis hid_base

// Construct deflated transfer amount vars for imbalanced baseline values

foreach x in trans_from_tot 	    trans_from_ind {
loc labl: var label `x'
	replace `x'=`x'/1000
	label var `x' "`labl' (1000s TSH)"
	
	clonevar `x'_09cons=`x'
	replace `x'_09cons=`x'_09cons/1.196777893 if period==2
	replace `x'_09cons=`x'_09cons/1.388277649 if period==3
	label var `x'_09cons "`labl' in 1000s cons. 2009 TSH"
}

foreach x in value_gift_tot		value_gift_cash 		value_gift_food		value_gift_inkind {
loc labl: var label `x'
	replace `x'=`x'/1000
	label var `x' "`labl' (1000s TSH)"
	
	clonevar `x'_09cons=`x'
	replace `x'_09cons=`x'_09cons/1.196777893 if period==2
	replace `x'_09cons=`x'_09cons/1.388277649 if period==3
	label var `x'_09cons "`labl' in 1000s cons. 2009 TSH"
	
	winsor2 `x'_09cons, cuts(0 99)
	rename `x'_09cons_w `x'_09cons_w1
	label var `x'_09cons_w1 "`x'_09cons with top 1% of obs. winsorized"
}

// OUTCOMES AND CONTROL VECTORS (VARIABLES IMBALANCED AT BASELINE)
loc outcomes1	= "value_gift_tot_09cons		value_gift_cash_09cons 		value_gift_food_09cons		value_gift_inkind_09cons "
loc outcomes1a	= "value_gift_cash_09cons 		value_gift_food_09cons		value_gift_inkind_09cons "
loc outcomes2	= "value_gift_tot_09cons_w1		value_gift_cash_09cons_w1 		value_gift_food_09cons_w1		value_gift_inkind_09cons_w1 "
loc imbalancedvars = "improvedfloor_b	dm_trust_comm	dm_food_from_ind	trans_from_tot_09cons	trans_from_ind_09cons	"


// REGRESSIONS

//without HTE, controlling for variables imbalanced at baseline interacted with a time trend

foreach y in `imbalancedvars' {
	clonevar temp= `y'
	replace temp=. if period!=1
	bysort hid_base: egen `y'_b=max(temp)
	loc labb: var label `y'
	gen im_`y'_b_pd2=`y'_b*dmpd2
	label var im_`y'_b_pd2 "BL val of `labb' x pd 2"
	gen im_`y'_b_pd3=`y'_b*dmpd3
	label var im_`y'_b_pd3 "BL val of `labb' x pd 3"
	drop temp 
}

qui foreach y in `outcomes1' `outcomes2' {
	eststo: xtreg 	`y'		assignedtotreat2 	assignedtotreat3	dmpd2	dmpd3 	im_*,  cluster(CLID)	fe
}
noi estout *, cells(	b(fmt(%9.0f)star)	se(fmt(%9.0f)) 	) 	stats(N r2,fmt(0 3)) starlevels(* 0.10 ** 0.05 *** 0.01) 

esttab using Table6_and_TableA3.csv, b(2) se(3) r2 starlevels(* 0.1 ** .05 *** .01) obslast replace


//Set up to run with HTE, controlling for variables imbalanced at baseline interacted with a time trend

est clear

foreach k in   		  assignedtotreat2 	assignedtotreat3 dmpd2 dmpd3 	im_improvedfloor_b_b_pd2 im_improvedfloor_b_b_pd3 im_dm_trust_comm_b_pd2 im_dm_trust_comm_b_pd3 im_dm_food_from_ind_b_pd2 im_dm_food_from_ind_b_pd3 im_trans_from_tot_09cons_b_pd2 im_trans_from_tot_09cons_b_pd3 im_trans_from_ind_09cons_b_pd2 im_trans_from_ind_09cons_b_pd3 {
	loc labl: var label `k'
	gen	I`k'		=	dm_poor*`k'	//"I" is for "interact"
	lab var I`k' "`labl' X dummy - poor"
}
loc labb: var label value_gift_tot_09cons
xtreg value_gift_tot_09cons assignedtotreat2 assignedtotreat3 dmpd2 dmpd3 Iassignedtotreat2 Iassignedtotreat3 Idmpd2 Idmpd3 ///
 	im_* 	Iim_* , cluster(CLID) fe
	
forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}	
outreg2 using TableA6.xls, bdec(3) pdec(11)  stats(coef se) label replace ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2',p-value of difference (midline) , `pvaldiff2', ///
		linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3', p-value of difference (endline) , `pvaldiff3')	



//Heterogeneous treatment effects by baseline asset wealth poverty

est clear
foreach y in `outcomes1a'	{
	loc labb: var label `y'
	xtreg 	`y'		 assignedtotreat2 	 assignedtotreat3	 dmpd2	 dmpd3	///
					Iassignedtotreat2 	Iassignedtotreat3	Idmpd2	Idmpd3	im_* 	Iim_* ,	cluster(CLID)	fe


	forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}
	
	outreg2 using TableA6.xls, bdec(3) pdec(11)  stats(coef se) label append ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2',p-value of difference (midline) , `pvaldiff2', ///
		linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3', p-value of difference (endline) , `pvaldiff3')	

}


*Attrition summary stats

keep if period==1
sort hid_base
count if hid_base==hid_base[_n-1]
count

*Include only households actually surveyed at baseline (i.e., omit the 4 households of the 1764 that were NOT surveyed at baseline but should have been); gives a more proper measure of 'atrition SINCE baseline')
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==0
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==1
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==0
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==1

count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==0 & treat_village==1
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==1 & treat_village==1
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==0 & treat_village==1
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==1 & treat_village==1

count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==0 & treat_village==0
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_2==1 & treat_village==0
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==0 & treat_village==0
count if dm_noatrit_HH_p1_1==1 & dm_noatrit_HH_p1_3==1 & treat_village==0


*Baseline means of outcomes for Tables 6 and A3:

summ value_gift_tot_09cons		value_gift_cash_09cons 		value_gift_food_09cons		value_gift_inkind_09cons

summ value_gift_tot_09cons_w1		value_gift_cash_09cons_w1 		value_gift_food_09cons_w1		value_gift_inkind_09cons_w1

exit
